package com.yango.teach.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.commonutil.result.Result;
import com.yango.teach.entity.Teacher;
import com.yango.teach.entity.vo.TeacherQuery;
import com.yango.teach.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-21
 */
@Api(description = "讲师 前端控制器")
@RestController
@RequestMapping("/teach/teacher")
public class TeacherController {

    //注入service
    @Autowired
    private TeacherService teacherService;

    //1 查询所有讲师
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/findAll")
    public Result findAll() {
        List<Teacher> list = teacherService.list(null);
        return Result.ok().data("teacherList", list);
    }

    //2 逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@ApiParam(value = "讲师ID", name = "id", required = true)
                             @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return Result.ok().message("删除讲师成功!");
        } else {
            return Result.error().message("删除讲师失败!");
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/pageQueryList/{current}/{limit}")
    public Result pageQueryList(@ApiParam(name = "current", value = "当前页", required = true)
                                @PathVariable long current,
                                @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                                @PathVariable long limit,
                                @ApiParam(name = "teacherQuery", value = "条件查询对象", required = false)
                                @RequestBody TeacherQuery teacherQuery) {
        //创建page对象,传入当前页和每页记录数
        Page<Teacher> page = new Page<>(current, limit);
        teacherService.pageQueryList(page, teacherQuery);//调用分页查询方法
        List<Teacher> records = page.getRecords();//记录集合
        long total = page.getTotal();//总记录数
        return Result.ok().data("items", records).data("total", total);
    }

    //4 新增讲师
    @ApiOperation(value = "新增讲师")
    @PostMapping("/saveTeacher")
    public Result saveTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                              @RequestBody Teacher teacher) {
        boolean flag = teacherService.save(teacher);
        if (flag) {
            return Result.ok().message("新增讲师成功!");
        } else {
            return Result.error().message("新增讲师失败!");
        }
    }

    //5 根据讲师ID查询讲师
    @ApiOperation(value = "根据讲师ID查询讲师")
    @GetMapping("/getTeacherById/{id}")
        public Result getTeacherById(@ApiParam(name = "id", value = "讲师ID", required = true)
                                 @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            return Result.ok().message("没有查询到讲师信息!");
        } else {
            return Result.ok().data("teacher", teacher);
        }
    }

    //6 修改讲师
    @ApiOperation(value = "修改讲师")
    @PutMapping("/updateTeacher")
    public Result updateTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                                @RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return Result.ok().message("修改讲师成功!");
        } else {
            return Result.error().message("修改讲师失败!");
        }
    }


}

